package edu.ncsu.csc.itrust.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import edu.ncsu.csc.itrust.DBUtil;
import edu.ncsu.csc.itrust.beans.LabProcedureBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.exception.DBException;

/**
 * DAO for Lab Procedures associated with Office Visits
 * @author Patrick Finegan & Bailey Hayes
 *
 */
public class OfficeVisitLabProcedureDAO {

	/** Obligatory parent object */
	private DAOFactory factory;
	
	/**
	 * The typical constructor.
	 * @param factory The {@link DAOFactory} associated with this DAO, which is used for obtaining SQL connections, etc.
	 */
	public OfficeVisitLabProcedureDAO(DAOFactory factory) {
		this.factory = factory;
	}
	
	/**
	 * Adds a LOINCCode to an office visit.
	 * 
	 * @param LOINCCode A string of the code being added.
	 * @param visitID The ID of the office visit the code is being added to.
	 * @param pid The patient's MID associated with this transaction.
	 * @return The unique ID of the code that was added.
	 * @throws DBException
	 */
	public long addLabProcedureToOfficeVisit(String LOINCCode, long visitID, long pid) throws DBException {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = factory.getConnection();
			ps = conn.prepareStatement("INSERT INTO LabProcedure (LaboratoryProcedureCode,OfficeVisitID," +
					"Commentary, Results, PatientMID, Status, Rights) VALUES (?,?,?,?,?,?,?)");
			ps.setString(1, LOINCCode);
			ps.setLong(2, visitID);
			ps.setString(3, "");
			ps.setString(4, "");
			ps.setLong(5, pid);
			ps.setString(6, LabProcedureBean.In_Transit);
			ps.setString(7, "ALLOWED");
			ps.executeUpdate();
			return DBUtil.getLastInsert(conn);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DBException(e);
		} finally {
			DBUtil.closeConnection(conn, ps);
		}
	}
	
	/**
	 * Removes a given lab procedure.
	 * 
	 * @param labProcedureID The unique ID of the lab procedure.
	 * @throws DBException
	 */
	public void removeLabProcedureFromOfficeVisit(long labProcedureID) throws DBException {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = factory.getConnection();
			ps = conn.prepareStatement("DELETE FROM LabProcedure WHERE LaboratoryProcedureID=?");
			ps.setLong(1, labProcedureID);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DBException(e);
		} finally {
			DBUtil.closeConnection(conn, ps);
		}
	}
	
	/**
	 * Edits an existing lab procedure bean.
	 * 
	 * @param lproc The newly updated lab procedure bean.
	 * @return A long indicating the ID of the newly updated lab procedure bean.
	 * @throws DBException
	 */
	public long editLabProcedure(LabProcedureBean lproc) throws DBException {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = factory.getConnection();
			String statement = "UPDATE LabProcedure " +
				"SET LaboratoryProcedureCode=?, Rights=?, Status=?, Commentary=? WHERE LaboratoryProcedureID=?";
			ps = conn.prepareStatement(statement);
			ps.setString(1, lproc.getLoinc());
			ps.setString(2, lproc.getRights());
			ps.setString(3, lproc.getStatus());
			ps.setString(4, lproc.getCommentary());
			ps.setLong(5, lproc.getProcedureID());
			ps.executeUpdate();
			return DBUtil.getLastInsert(conn);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DBException(e);
		} finally {
			DBUtil.closeConnection(conn, ps);
		}
	}
	
}
